const path = require('path')
const ejs = require('ejs')

async function compileEjs(templateName, data) {
  return new Promise((resolve, reject) => {
    // 获取当前模板路径
    const templatePath = `../template/${templateName}`
    const absolutePath = path.resolve(__dirname, templatePath)

    // 编译模板文件
    ejs.renderFile(absolutePath, data, (err, result) => {
      if (err) {
        console.log('ejs编译模板失败：', err)
        reject(err)
        return
      }
      resolve(result)
    })
  })

}

module.exports = compileEjs